package com.ymatou.iapi.livemanage.testcase;

import java.util.Date;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livemanage.service.CloseActivityCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.LiveBasicInfoWapper;
import com.ymttest.database.mongo.LiveModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class Tc_CloseActivity {
	
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播Admin-结束直播");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new CloseActivityCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	
	 @Test
	 @Category(P1.class)
	 @TestCase
	public void Tc_CloseActivity_001() {
		 Logger.start(true,"直播Admin-结束直播");
		 try {
			 
			 int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");			 
			 
			 Logger.verifyEquals(200, LiveCallService.closeLive(liveId,sellerId).getInt("Code"),  "响应报文-返回码");	
			 
			 
			 //check SQL
			 Map<String,Object> ret = MapUtil.pojoToMap(new YmtLiveWapper().selectByLiveId(liveId));
			 Logger.verifyEquals(1, YMTDateUtil.getDate().compareTo((Date)ret.get("dendtime")),"结束时间应小于当前时间 SQL对比");
			 
			 //check Mongo
			 if (YmtMongoDriverFactroy.isOldMongoCheck()){
				 Map<String, Object>liveBasicInfoMap = new LiveBasicInfoWapper().getActivityId(liveId);
				 Logger.verifyEquals(1, YMTDateUtil.getDate().compareTo((Date)liveBasicInfoMap.get("EndTime")),"结束时间应小于当前时间 Mongo老库对比");
				 if (YMTDateUtil.getDate().compareTo((Date)liveBasicInfoMap.get("EndTime")) == -1) {
					Logger.debug(liveBasicInfoMap.get("EndTime"));
				}				 
			 }
			 
			 if (YmtMongoDriverFactroy.isNewMongoCheck()){
				 Map<String, Object>liveModelWapper = new LiveModelWapper().getActivityId(liveId);
				 Logger.verifyEquals(1, YMTDateUtil.getDate().compareTo((Date)liveModelWapper.get("EndTime")),"结束时间应小于当前时间 Mongo新库对比");
				 if (YMTDateUtil.getDate().compareTo((Date)liveModelWapper.get("EndTime")) == -1) {
					Logger.debug(liveModelWapper.get("EndTime"));
				}				 
			 }
			 
			 
			 
		 }catch (Exception e){
			 Logger.fail(e);
		 } 
	 }
	 
	 
	 
	 @Test
	 @Category(P1.class)
	 @TestCase
	public void Tc_CloseActivity_002() {
		 Logger.start(false,"直播Admin-结束直播 错误liveId 和错误sellerI的");
		 try {
			 
			 int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			 
			 Logger.verifyEquals(201, LiveCallService.closeLive(123,sellerId).getInt("Code"),  "响应报文-返回码");	
			 Logger.verifyEquals(201, LiveCallService.closeLive(liveId,222).getInt("Code"),  "响应报文-返回码");	
			 
			 //check Mongo
			 if (YmtMongoDriverFactroy.isOldMongoCheck()){
				 Map<String, Object>liveBasicInfoMap = new LiveBasicInfoWapper().getActivityId(liveId);
				 Logger.verifyEquals(-1, YMTDateUtil.getDate().compareTo((Date)liveBasicInfoMap.get("EndTime")),"结束时间应小于当前时间 Mongo老库对比");				 
			 }
			 
			 if (YmtMongoDriverFactroy.isNewMongoCheck()){
				 Map<String, Object>liveModelWapper = new LiveModelWapper().getActivityId(liveId);
				 Logger.verifyEquals(-1, YMTDateUtil.getDate().compareTo((Date)liveModelWapper.get("EndTime")),"结束时间应小于当前时间 Mongo新库对比");				
			 }
			
			 
		 }catch (Exception e){
			 Logger.fail(e);
		 } 
	 }
	 
	 @Test
	 @Category(P1.class)
	 @TestCase
	public void Tc_CloseActivity_003() {
		 Logger.start(false,"结束正在进行的直播（直播商品销售中）");
		 try {
			 
			 //1 add live
			 int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			 
			//2 add new product
			 JSONObject productBean = SellerProductCallService.addNewProductNotOnSale(sellerId,sellerName,null);
			 String productId =	productBean.getJSONObject("Data").getString("ProductID");
			
			 
			//3 add product to live
	    	JSONObject addProductInLiveBean = SellerProductCallService.addProductInLive(sellerId,liveId,productId);
			
			 //4 close live
			 Logger.verifyEquals(200, LiveCallService.closeLive(liveId,sellerId).getInt("Code"),  "响应报文-返回码");	
			 
			 //check Mongo			 			 
			 if (YmtMongoDriverFactroy.isNewMongoCheck()){
				 Map<String, Object>liveModelWapper = new LiveModelWapper().getActivityId(liveId);
				 Logger.verifyEquals(1, YMTDateUtil.getDate().compareTo((Date)liveModelWapper.get("EndTime")),"结束时间应小于当前时间 Mongo新库对比");
				 if (YMTDateUtil.getDate().compareTo((Date)liveModelWapper.get("EndTime")) == -1) {
					Logger.debug(liveModelWapper.get("EndTime"));
				}				 
			 }
			 
			 
		 }catch (Exception e){
			 Logger.fail(e);
		 } 
	 }	
	

	 
	 
}
